﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;

using Microsoft.Practices.Prism.ViewModel;

using Finance.Entites;

namespace Finance.UI.ViewModels
{
    public class PeriodicDetailPageViewModel : NotificationObject
    {
        public ObservableCollection<PeriodicDetailListViewModel> Lists
        {
            get;
            private set;
        }

        public PeriodicDetailPageViewModel(Period period)
        {
            Lists = new ObservableCollection<PeriodicDetailListViewModel>();
            if (App.ChargeDataStore.Items == null || App.ChargeDataStore.Items.Count == 0)
            {
                return;
            }
            var minDate = App.ChargeDataStore.Items.Min<Charge, DateTime>(c => c.Date);
            var maxDate = App.ChargeDataStore.Items.Max<Charge, DateTime>(c => c.Date);

            //Set the interval of how to get the next recent time point
            Func<DateTime, DateTime> Interval = default(Func<DateTime, DateTime>);
            switch (period)
            {
                case Period.Day:
                    {
                        Interval = d => d.AddDays(-1);
                    }
                    break;
                case Period.Week:
                    {
                        Interval = d => d.AddDays(-7);
                    }
                    break;
                case Period.Month:
                    {
                        Interval = d => d.AddMonths(-1);
                    }
                    break;
                default: break;
            }

            //Temporarily unused
            IList<PeriodEventArgs> periodEventArgs = new List<PeriodEventArgs>();
            //Set the start date and end date of a specify period
            const int MAXCOUNT = 10;
            int idx = 0;
            for (var date = DateTime.Today; date >= minDate.AddDays(-6) && idx < MAXCOUNT; date = Interval(date), idx++)
            {
                DateTime startdate = DateTime.Now;
                DateTime enddate = DateTime.Now;
                switch (period)
                {
                    case Period.Day:
                        {
                            startdate = date;
                            enddate = date;
                        }
                        break;
                    case Period.Week:
                        {
                            startdate = date.AddDays((-((int)date.DayOfWeek + 6) % 7));
                            enddate = startdate.AddDays(6);
                        }
                        break;
                    case Period.Month:
                        {
                            startdate = new DateTime(date.Year, date.Month, 1);
                            enddate = new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1);
                        }
                        break;
                    default: break;
                }
                //Check whether any item exsit in the data store, if true then add a new pivot item to control
                if (App.ChargeDataStore.Items.Any(c => (c.Date > startdate && c.Date < enddate.AddDays(1)))
                    || (DateTime.Today >= startdate && DateTime.Today < enddate))
                {
                    string header =
                        startdate == enddate ?
                        startdate.ToShortDateString()
                        : string.Format("{0} ~ {1}", startdate.ToShortDateString(), enddate.ToShortDateString());
                    Lists.Add(new PeriodicDetailListViewModel(header, startdate, enddate));
                }
            }
        }

        public long ToInt64(DateTime datetime)
        {
            return datetime.Year * 10000000 + datetime.Month * 100 + datetime.Day;
        }

        public class PeriodEventArgs : EventArgs
        {
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
        }
    }
}
